package com.shanshan.crowd.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.shanshan.crowd.constant.CrowdConstant;
import com.shanshan.crowd.entity.Admin;
import com.shanshan.crowd.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @Description
 * @ClassName AdminController
 * @Author 山山大魔王
 * @Date 2022/7/20 21:20
 * @Version 1.0
 */
@Controller
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequestMapping("/admin/update.html")
    public String update(Admin admin,
                         @RequestParam("pageNum") Integer pageNum,
                         @RequestParam("keyword") String keyword){
        adminService.update(admin);

        return "redirect:/admin/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }

    @RequestMapping("/admin/to/edit/page.html")
    public String toEditPage(@RequestParam("adminId") Integer adminId,
                            ModelMap model){

        // 1.更具id查询待更新的Admin对象
        Admin admin = adminService.getAdminById(adminId);
        // 2.将Admin对象存入模型
        String adminStr = "admin";
        model.addAttribute(adminStr, admin);
        return "admin-edit";
    }



    @PreAuthorize("hasAuthority('user:save')")
    @RequestMapping("/admin/save.html")
    public String saveAdmin(Admin admin){
        // 执行保存
        adminService.saveAdmin(admin);
        // 重定向到分页页面，使用重定向是为了避免重复提交表单
        return "redirect:/admin/page.html?pageNum="+Integer.MAX_VALUE;
    }

    @RequestMapping("/admin/remove.html")
    public String remove(@RequestParam("adminId") Integer adminId,
                         @RequestParam("pageNum") Integer pageNum,
                         @RequestParam("keyword") String keyword){
        //执行删除
        adminService.remove(adminId);

        // return "admin-page"; 会无法显示分页数据
        // return "forward:/admin/get/page.html" 一旦刷新会重复执行删除，浪费性能
        // return "redirect:/admin/get/page.html?pageNum=&keyword="
        // 页面的跳转
        return "redirect:/admin/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }


    @RequestMapping("/admin/do/login.html")
    public String doLogin(
            @RequestParam("loginAcct") String loginAcct,
            @RequestParam("userPswd") String userPswd,
            HttpSession session
    ){
        // 调用Service方法执行登录状态检查
        // 这个方法如果能够返回admin对象说明登录成功，如果账号、密码不正确则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct,userPswd);

        // 将登录成功返回的admin对象存入session域
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);

        return "redirect:/admin/to/main/page.html";
    }

    @RequestMapping("/admin/do/logout.html")
    public String doLogout(HttpSession session){
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }

    @RequestMapping("/admin/page.html")
    public String getAdminPage(
            @RequestParam(value = "keyword",defaultValue = "") String keyword,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            ModelMap modelMap
    ){
        //查询分页数据
        PageInfo<Admin> pageInfo = adminService.getAdminPage(keyword, pageNum, pageSize);

        //将分页数据存入模型
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);

        return "admin-page";
    }

}
